﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;


namespace Excel2Word
{
    public class WordOperation
    {

        public const string FIELD_TAG = "SRFA";


        /// <summary>
        /// 获得用户打开的当前活动状态WORD进程
        /// </summary>
        /// <returns></returns>
        // Word.Application
        public static dynamic GetActiveWord()
        {
            dynamic _app = null;
            try
            {
                _app = (dynamic)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
            }
            catch
            {
            }
            return _app;

        }

        /// <summary>
        /// 根据
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public static dynamic GetDocByFullName(string fullName,bool createNewApp = false)
        {
            dynamic objWord =createNewApp ? null : GetActiveWord();
            dynamic objDoc = null;
            try
            {
                if (objWord == null)
                {
                    
                    Type type = Type.GetTypeFromProgID("Word.Application");
                    objWord = Activator.CreateInstance(type);  //new Microsoft.Office.Interop.Word.Application();
                    objWord.Visible = true;

                }
                else
                {
                    //Word.Document
                    foreach (dynamic doc in objWord.Documents)
                    {
                        if (doc.FullName == fullName)
                        {
                            return doc;
                        }
                    }
                }
                //objWord.Visible = true;
                //Word.WdAlertLevel.wdAlertsAll
                objWord.DisplayAlerts = -1;//Microsoft.Office.Interop .Word.WdAlertLevel.wdAlertsAll;

                if (objDoc == null) return objWord.Documents.Open(fullName);
            }
            catch { }

            return null;
        }

        /// <summary>
        /// 判断某个word文件是否已打开
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public static dynamic  DocumentIsOpen(string fullName)
        {
            dynamic objWord = GetActiveWord();
            if (objWord == null)
            {
                
                return null;
            }
            else
            {
                foreach (dynamic doc in objWord.Documents)
                {
                    if (doc.FullName == fullName)
                    {
                        return doc;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 更新一个word文档中，所有的域
        /// </summary>
        /// <param name="doc"></param>
        public static void UpdataAllFieldsInDoc(dynamic doc,Dictionary<string,string> varDic)
        {
            //Word.Variable
            foreach (dynamic objVar in doc.Variables)
            {
                //判断变量是否为本应用的VAR
                if (objVar.Name.StartsWith(FIELD_TAG + "_"))
                {
                    string varName = objVar.Name.Split('_')[1];
                    if (varDic.ContainsKey(varName))
                    {
                        objVar.Value = varDic[varName];
                    }
                }
            }

            doc.Fields.Update();
        }




    }
}
